package logic

import (
	"2207a/bawei/api/internal/svc"
	"2207a/bawei/api/internal/types"
	"2207a/bawei/api/pkg"
	"2207a/bawei/user_srv/user"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SendSmsLogic) SendSms(req *types.SendSmsRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	if !pkg.ValidateMobile(req.Mobile) {
		return &types.Response{
			Code:    -4,
			Message: "手机号码格式错误",
			Data:    nil,
		}, nil
	}

	Limit, err := l.svcCtx.UserSrv.SendLimit(l.ctx, &user.SendLimitRequest{Mobile: req.Mobile})
	if err != nil {
		return &types.Response{
			Code:    -2,
			Message: "发送短信限流错误",
			Data:    nil,
		}, nil
	}
	if Limit.IsLimit == false {
		return &types.Response{
			Code:    -3,
			Message: "限制短信发送频率,只能每分钟一次",
			Data:    nil,
		}, nil
	}
	code := pkg.RandOmInfo()
	//pkg.SensSms(req.Mobile, code)
	_, err = l.svcCtx.UserSrv.CacheSendSms(l.ctx, &user.CacheSendSmsRequest{
		Mobile: req.Mobile,
		Source: req.Source,
		Code:   code,
	})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: "发送短信错误",
			Data:    nil,
		}, nil
	}
	//redis.DelSend(req.Mobile, "Login")

	return &types.Response{
		Code:    200,
		Message: "发送短信成功",
		Data:    nil,
	}, nil
}

//20*3=60
